class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def reverseList(head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    if not head or not head.next:
        return head
    P = reverseList(head.next)
    head.next.next = head
    head.next = None
    return P


node = ListNode(1)
node.next = ListNode(2)
node.next.next = ListNode(3)
node.next.next.next = ListNode(4)
node.next.next.next.next = ListNode(5)
node = reverseList(node)
print(node.val)
print(node.next.val)
print(node.next.next.val)
print(node.next.next.next.val)
print(node.next.next.next.next.val)
